package com.vic.mvc.prepare.design.proxy.cglib;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class LogTrain implements MethodInterceptor{

	public Object getProxy(Class<?> clazz){
		Enhancer e = new Enhancer();
		//设置创建子类的类
		e.setSuperclass(clazz);
		e.setCallback(this);
		return e.create();
	}
	
	/**
	 * 拦截所有目标方法的调用
	 * obj 目标列的实例
	 * method 目标对象的反射对象
	 * args 方法的参数
	 * proxy 代理类的实例 
	 */
	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		System.out.println("火车要到站了");
		proxy.invokeSuper(obj, args);
		System.out.println("火车出站了");
		return null;
	}

}
